<?php

class Core_Backend_Component_Road_View_Info_Edit extends Core_Backend_Share_View_Form {

    public function __construct() {
        parent::__construct();
        $this->setTemplatePath('edit');
    }

    protected function buildNavigation($navigation) {
        $navigation->addLabel($this->__f('road.title.info_edit', $this->getModel()->getRoadName()));
        parent::buildNavigation($navigation);
    }

    protected function buildToolbar($toolbar) {
    	$group = $toolbar->createGroup('back');
        $group->addLink($this->getHistoryUrl('road/info/index'), $this->__('share.button.back'));
        $group = $toolbar->createGroup('save');
        $group->addButton($this->__('share.button.apply'))
              ->bindEvent('click', "Kdf.Ui.getForm('".$this->getForm()->getId()."').submit('".$this->getUrl(NULL, 'apply')."')");
        $group->addButton($this->__('share.button.save'))
              ->bindEvent('click', "Kdf.Ui.getForm('".$this->getForm()->getId()."').submit('".$this->getUrl(NULL, 'save')."')");
        parent::buildToolBar($toolbar);
    }

    public function buildForm($form) {
        $entity = $this->getModel(NULL);
        $this->getTemplate()->assign('model', $entity);
        $form->setAction($this->getUrl())
                ->setId('info_form')
                ->addHiddenField('id', 'info[id]');
        $form->addTextboxField($this->__('road.label.alias'), 'alias', 'info[alias]');
        $form->addTextboxField($this->__('road.label.precinct'), 'precinct', 'info[precinct]');
        $form->addTextboxField($this->__('road.label.traffic'), 'traffic', 'info[traffic]');
        $form->addTextboxField($this->__('road.label.feature'), 'feature', 'info[feature]');
        $form->addPoiBrowserField($this->__('road.label.landmark'), 'landmark_ids', 'info[landmark_ids][]');
        $form->addPoiBrowserField($this->__('road.label.recommend_poi'), 'recommend_poi_ids', 'info[recommend_poi_ids][]', FALSE, $this->getRequest()->getParameter('code'));
        $form->addMultitextField($this->__('road.label.recommend_concessions'), 'recommend_concessions_ids', 'info[recommend_concessions_ids][]');
	$form->addEditorField($this->__('share.label.detail'), 'detail', 'info[detail]');
        if (!is_null($entity)) {
            $form->bindData($entity->getAllData(), array('alias', 'precinct', 'traffic', 'feature', 'detail'));
            if($this->getModel()->issetData('landmark_ids')){
            	$form->getInput('landmark_ids')->setValue($this->getModel()->getLandmarks()->toArray());
            }
            if($this->getModel()->issetData('recommend_poi_ids')){
            	$form->getInput('recommend_poi_ids')->setValue($this->getModel()->getRecommendPois()->toArray());
            }
            if($this->getModel()->issetData('recommend_concessions_ids')){
            	$form->getInput('recommend_concessions_ids')->setValue($this->getModel()->getRecommendConcessions()->toArray());
            }
        }
        $form->addHiddenField('code', 'info[code]', $this->getModel()->getRoadId());
        parent::buildForm($form);
    }

}

